package com.ttnet.listeners;

import java.io.Serializable;
import java.util.Date;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

import com.ttnet.model.BaseModel;

public class EntityInterceptor extends EmptyInterceptor {

	public void onDelete(Object entity, Serializable id, Object[] state,
			String[] propertyNames, Type[] types) {
		// do nothing
	}

	public boolean onSave(Object entity, Serializable id, Object[] state,
			String[] propertyNames, Type[] types) {

		if (entity instanceof BaseModel) {
			for (int i = 0; i < propertyNames.length; i++) {
				if ("createTime".equals(propertyNames[i])) {
					state[i] = new Date();
					return true;
				}

			}
		}
		return false;

	}

	public boolean onFlushDirty(Object entity, Serializable id,
			Object[] currentState, Object[] previousState,
			String[] propertyNames, Type[] types) {

		if (entity instanceof BaseModel) {
			for (int i = 0; i < propertyNames.length; i++) {
				if ("lastUpdateTime".equals(propertyNames[i])) {
					currentState[i] = new Date();
					return true;
				}
			}
		}
		return false;

	}

}